home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / Z5IZ1E (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.0 KB  |  270 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.Icon;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JSlider;
  6. import com.sun.java.swing.UIManager;
  7. import com.sun.java.swing.plaf.ComponentUI;
  8. import com.sun.java.swing.plaf.basic.BasicSliderUI;
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.awt.Rectangle;
  12. import java.beans.PropertyChangeEvent;
  13. import java.beans.PropertyChangeListener;
  14.  
  15. public class MetalSliderUI extends BasicSliderUI implements PropertyChangeListener {
  16.    protected final int TICK_BUFFER = 4;
  17.    protected boolean filledSlider = false;
  18.    protected static Color thumbColor;
  19.    protected static Color highlightColor;
  20.    protected static Color darkShadowColor;
  21.    protected static int trackWidth;
  22.    protected static int tickLength;
  23.    protected static Icon horizThumbIcon;
  24.    protected static Icon vertThumbIcon;
  25.    protected final String SLIDER_FILL = "JSlider.isFilled";
  26.  
  27.    public MetalSliderUI() {
  28.       super((JSlider)null);
  29.    }
  30.  
  31.    public void calculateThumbBounds() {
  32.       int thumbLength = 16;
  33.       if (super.slider.getOrientation() == 0) {
  34.          int thumbX = ((BasicSliderUI)this).xPositionForValue(super.slider.getValue()) - ((BasicSliderUI)this).getThumbRect().width / 2;
  35.          int thumbY = ((BasicSliderUI)this).getScrollTrackRect().y + ((BasicSliderUI)this).getScrollTrackRect().height - thumbLength;
  36.          ((BasicSliderUI)this).setThumbBounds(thumbX, thumbY, 15, thumbLength);
  37.       } else {
  38.          int thumbY = ((BasicSliderUI)this).yPositionForValue(super.slider.getValue()) - ((BasicSliderUI)this).getThumbRect().height / 2;
  39.          int thumbX = ((BasicSliderUI)this).getScrollTrackRect().x + ((BasicSliderUI)this).getScrollTrackRect().width - thumbLength;
  40.          ((BasicSliderUI)this).setThumbBounds(thumbX, thumbY, thumbLength, 15);
  41.       }
  42.  
  43.    }
  44.  
  45.    public static ComponentUI createUI(JComponent c) {
  46.       return new MetalSliderUI();
  47.    }
  48.  
  49.    protected int getThumbOverhang() {
  50.       return 4;
  51.    }
  52.  
  53.    public int getTickSpace() {
  54.       return super.slider.getOrientation() == 0 ? tickLength + 4 + 1 : tickLength + 4 + 3;
  55.    }
  56.  
  57.    protected int getTrackLength() {
  58.       ((BasicSliderUI)this).getFullContentArea();
  59.       return super.slider.getOrientation() == 0 ? ((BasicSliderUI)this).getScrollTrackRect().width - super.trackBuffer * 2 : ((BasicSliderUI)this).getScrollTrackRect().height - super.trackBuffer * 2;
  60.    }
  61.  
  62.    protected int getTrackWidth() {
  63.       return trackWidth;
  64.    }
  65.  
  66.    public void installUI(JComponent c) {
  67.       trackWidth = (Integer)UIManager.get("Slider.trackWidth");
  68.       tickLength = (Integer)UIManager.get("Slider.majorTickLength");
  69.       horizThumbIcon = UIManager.getIcon("Slider.horizontalThumbIcon");
  70.       vertThumbIcon = UIManager.getIcon("Slider.verticalThumbIcon");
  71.       super.installUI(c);
  72.       thumbColor = UIManager.getColor("Slider.thumb");
  73.       highlightColor = UIManager.getColor("Slider.highlight");
  74.       darkShadowColor = UIManager.getColor("Slider.darkShadow");
  75.       super.scrollListener.setScrollByBlock(false);
  76.       c.addPropertyChangeListener(this);
  77.       Object sliderFillProp = c.getClientProperty("JSlider.isFilled");
  78.       if (sliderFillProp != null) {
  79.          this.filledSlider = (Boolean)sliderFillProp;
  80.       }
  81.  
  82.    }
  83.  
  84.    public void paintFocus(Graphics g) {
  85.       if (super.slider.hasFocus()) {
  86.          Rectangle r = super.slider.getBounds();
  87.          r.x = 0;
  88.          r.y = 0;
  89.          if (super.slider.getBorder() != null) {
  90.             r = ((BasicSliderUI)this).getFullContentArea();
  91.          }
  92.  
  93.          g.setColor(((BasicSliderUI)this).getFocusColor());
  94.          g.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2);
  95.       }
  96.  
  97.    }
  98.  
  99.    protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
  100.       g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
  101.       g.drawLine(x, 4, x, 4 + (tickLength - 1));
  102.    }
  103.  
  104.    protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) {
  105.       g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
  106.       g.drawLine(4, y, 4 + tickLength, y);
  107.    }
  108.  
  109.    protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) {
  110.       g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
  111.       g.drawLine(x, 4, x, 4 + tickLength / 2);
  112.    }
  113.  
  114.    protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) {
  115.       g.setColor((Color)(super.slider.isEnabled() ? super.slider.getForeground() : MetalLookAndFeel.getControlShadow()));
  116.       g.drawLine(4, y, 4 + tickLength / 2, y);
  117.    }
  118.  
  119.    public void paintThumb(Graphics g) {
  120.       Rectangle knobBounds = ((BasicSliderUI)this).getThumbRect();
  121.       g.translate(knobBounds.x, knobBounds.y);
  122.       if (super.slider.getOrientation() == 0) {
  123.          horizThumbIcon.paintIcon(super.slider, g, 0, 0);
  124.       } else {
  125.          vertThumbIcon.paintIcon(super.slider, g, 0, 0);
  126.       }
  127.  
  128.       g.translate(-knobBounds.x, -knobBounds.y);
  129.    }
  130.  
  131.    public void paintTrack(Graphics g) {
  132.       Rectangle trackBounds = ((BasicSliderUI)this).getScrollTrackRect();
  133.       Color trackColor = (Color)(!super.slider.isEnabled() ? MetalLookAndFeel.getControlShadow() : super.slider.getForeground());
  134.       g.translate(trackBounds.x, trackBounds.y);
  135.       int trackLeft = 0;
  136.       int trackTop = 0;
  137.       int trackRight = 0;
  138.       int trackBottom = 0;
  139.       if (super.slider.getOrientation() == 0) {
  140.          trackLeft = super.trackBuffer;
  141.          trackTop = trackBounds.height - this.getThumbOverhang() - this.getTrackWidth();
  142.          trackRight = trackBounds.width - 1 - super.trackBuffer;
  143.          trackBottom = trackBounds.height - this.getThumbOverhang() - 1;
  144.          if (super.slider.isEnabled()) {
  145.             MetalUtils.drawFlush3DBorder(g, trackLeft, trackTop, this.getTrackLength(), this.getTrackWidth());
  146.          } else {
  147.             Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
  148.             int middleOfThumb = thumbBounds.x + thumbBounds.width / 2;
  149.             g.setColor(trackColor);
  150.             g.drawRect(trackLeft, trackTop, this.getTrackLength(), this.getTrackWidth() - 2);
  151.             if (this.filledSlider) {
  152.                if (super.slider.isEnabled()) {
  153.                   g.fillRect(trackLeft, trackTop, middleOfThumb - trackLeft, this.getTrackWidth() - 2);
  154.                } else {
  155.                   g.fillRect(middleOfThumb, trackTop, trackRight - middleOfThumb, this.getTrackWidth() - 2);
  156.                }
  157.             }
  158.          }
  159.       } else {
  160.          trackLeft = trackBounds.width - this.getThumbOverhang() - this.getTrackWidth();
  161.          trackTop = super.trackBuffer;
  162.          trackRight = trackBounds.width - this.getThumbOverhang() - 1;
  163.          trackBottom = trackBounds.height - 1 - super.trackBuffer;
  164.          if (super.slider.isEnabled()) {
  165.             MetalUtils.drawFlush3DBorder(g, trackLeft, trackTop, this.getTrackWidth(), this.getTrackLength());
  166.          } else {
  167.             Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
  168.             int middleOfThumb = thumbBounds.y + thumbBounds.height / 2;
  169.             g.setColor(trackColor);
  170.             g.drawRect(trackLeft, trackTop, this.getTrackWidth() - 2, this.getTrackLength());
  171.             if (this.filledSlider) {
  172.                if (super.slider.isEnabled()) {
  173.                   g.fillRect(trackLeft, middleOfThumb, this.getTrackWidth() - 2, trackBottom - middleOfThumb);
  174.                } else {
  175.                   g.fillRect(trackLeft, trackTop, this.getTrackWidth() - 2, middleOfThumb - trackLeft);
  176.                }
  177.             }
  178.          }
  179.       }
  180.  
  181.       if (!this.filledSlider) {
  182.          if (super.slider.isEnabled()) {
  183.             g.setColor(super.slider.getForeground());
  184.             if (super.slider.getOrientation() == 0) {
  185.                g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackLength() - 4, this.getTrackWidth() - 4);
  186.                g.setColor(MetalLookAndFeel.getPrimaryControl());
  187.                g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2);
  188.                g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1);
  189.                g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  190.                g.drawLine(trackLeft + 2, trackBottom - 1, trackRight - 1, trackBottom - 1);
  191.                g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, trackBottom - 1);
  192.             } else {
  193.                g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackWidth() - 4, this.getTrackLength() - 4);
  194.                g.setColor(MetalLookAndFeel.getPrimaryControl());
  195.                g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 2);
  196.                g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2, trackTop + 1);
  197.                g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  198.                g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, trackBottom - 1);
  199.                g.drawLine(trackLeft + 2, trackBottom - 1, trackRight - 1, trackBottom - 1);
  200.             }
  201.          }
  202.       } else {
  203.          Color lightColor = MetalLookAndFeel.getPrimaryControl();
  204.          Color darkColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
  205.          g.setColor(super.slider.getForeground());
  206.          if (super.slider.getOrientation() == 0) {
  207.             Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
  208.             int middleOfThumb = thumbBounds.x + thumbBounds.width / 2;
  209.             if (!super.slider.getInverted()) {
  210.                g.fillRect(trackLeft + 2, trackTop + 2, middleOfThumb - (trackLeft + 2), this.getTrackWidth() - 4);
  211.                g.setColor(lightColor);
  212.                g.drawLine(trackLeft + 2, trackTop + 1, middleOfThumb, trackTop + 1);
  213.                g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1, trackBottom - 1);
  214.                g.setColor(darkColor);
  215.                g.drawLine(trackLeft + 2, trackBottom - 1, middleOfThumb, trackBottom - 1);
  216.             } else {
  217.                g.fillRect(middleOfThumb, trackTop + 2, trackRight - 1 - middleOfThumb, this.getTrackWidth() - 4);
  218.                g.setColor(lightColor);
  219.                g.drawLine(middleOfThumb, trackTop + 1, trackRight - 2, trackTop + 1);
  220.                g.drawLine(trackRight - 1, trackTop + 1, trackRight - 1, trackBottom - 1);
  221.                g.setColor(darkColor);
  222.                g.drawLine(middleOfThumb, trackBottom - 1, trackRight - 2, trackBottom - 1);
  223.             }
  224.          } else {
  225.             Rectangle thumbBounds = ((BasicSliderUI)this).getThumbRect();
  226.             int middleOfThumb = thumbBounds.y + thumbBounds.height / 2;
  227.             if (!super.slider.getInverted()) {
  228.                g.fillRect(trackLeft + 2, middleOfThumb, this.getTrackWidth() - 4, trackBottom - 1 - middleOfThumb);
  229.                g.setColor(lightColor);
  230.                g.drawLine(trackLeft + 1, middleOfThumb, trackLeft + 1, trackBottom - 2);
  231.                g.drawLine(trackLeft + 1, trackBottom - 1, trackRight - 1, trackBottom - 1);
  232.                g.setColor(darkColor);
  233.                g.drawLine(trackRight - 1, middleOfThumb, trackRight - 1, trackBottom - 2);
  234.             } else {
  235.                g.fillRect(trackLeft + 2, trackTop + 2, this.getTrackWidth() - 4, middleOfThumb - (trackTop + 2));
  236.                g.setColor(lightColor);
  237.                g.drawLine(trackLeft + 1, trackTop + 2, trackLeft + 1, middleOfThumb);
  238.                g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 1, trackTop + 1);
  239.                g.setColor(darkColor);
  240.                g.drawLine(trackRight - 1, trackTop + 2, trackRight - 1, middleOfThumb);
  241.             }
  242.          }
  243.       }
  244.  
  245.       g.translate(-trackBounds.x, -trackBounds.y);
  246.    }
  247.  
  248.    public void propertyChange(PropertyChangeEvent e) {
  249.       super.propertyChange(e);
  250.       String name = e.getPropertyName();
  251.       if (name.equals("JSlider.isFilled")) {
  252.          if (e.getNewValue() != null) {
  253.             this.filledSlider = (Boolean)e.getNewValue();
  254.          } else {
  255.             this.filledSlider = false;
  256.          }
  257.       }
  258.  
  259.    }
  260.  
  261.    protected void scrollDueToClickInTrack(int dir) {
  262.       ((BasicSliderUI)this).scrollByUnit(dir);
  263.    }
  264.  
  265.    public void uninstallUI(JComponent c) {
  266.       c.removePropertyChangeListener(this);
  267.       super.uninstallUI(c);
  268.    }
  269. }
  270.